package com.yiren.dbaa.module.project_edit.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.event.FragmentRefreshEvent;
import com.yiren.dbaa.currency.fragment.LazyLoadFragment;
import com.yiren.dbaa.module.project_edit.contract.SingleLightContract;
import com.yiren.dbaa.module.project_edit.presenter.SingleLightPresenter;
import com.yiren.dbaa.module.project_edit.view.activity.SingleLightEdit_Activity;
import com.yiren.dbaa.view.adapter.SingleLightAdapter;
import com.yiren.dbaa.view.dragsortlistview.DragSortListView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/8/24 10:27
 * @description: 单项灯光fragment
 */
public class SingleLight_Fragment extends LazyLoadFragment implements SingleLightContract.View {

    @BindView(R.id.dragsortlistview_listview)
    DragSortListView dragsortlistviewListview;
    @BindView(R.id.ll_no_singllight)
    LinearLayout llNoSingllight;

    private SingleLightContract.Presenter singleLightPresenter;
    private SingleLightAdapter singleLightAdapter;//单项灯光列表适配器
    private List<SingleLightEntity> singleLightEntityList;
    private SiteEntity site;

    @Override
    protected int setContentView() {
        return R.layout.fragment_single_light;
    }

    @Override
    protected void init() {
        site = (SiteEntity) getArguments().getSerializable("site");
        singleLightPresenter = new SingleLightPresenter(getActivity());
        singleLightPresenter.getSingleLightList(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        site = (SiteEntity) UserDbController.getInstance(false).queryById(site.getSiteCode(),SiteEntity.class);
        singleLightPresenter.getSingleLightList(this);
    }

    @OnClick(R.id.tv_add_singlelight)
    public void onClick() {
        Intent intent = new Intent(getActivity(), SingleLightEdit_Activity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("site", site);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void loadSingleLightList(List<SingleLightEntity> singleLightList) {
        singleLightEntityList = singleLightList;
        if (singleLightEntityList.size()<=0){
            dragsortlistviewListview.setVisibility(View.GONE);
            llNoSingllight.setVisibility(View.VISIBLE);
        }else {
            dragsortlistviewListview.setVisibility(View.VISIBLE);
            llNoSingllight.setVisibility(View.GONE);
            singleLightAdapter = new SingleLightAdapter(getActivity(), singleLightList, 1);
            dragsortlistviewListview.setAdapter(singleLightAdapter);
            //修改
            singleLightAdapter.setOnItemModifyClickListener(new SingleLightAdapter.OnItemModifyClickListener() {
                @Override
                public void onItemModifyClick(View view, int position) {
                    Intent intent = new Intent(getActivity(), SingleLightEdit_Activity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("singleLight", position);
                    bundle.putSerializable("site", site);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
            //删除
            singleLightAdapter.setOnItemDeleteClickListener(new SingleLightAdapter.OnItemDeleteClickListener() {
                @Override
                public void onItemDeleteClick(View view, int position) {
                    singleLightPresenter.deleteSinglLight(site, singleLightEntityList.get(position));
                }
            });
        }
    }

    @Override
    public void refreshAdapter(List<SingleLightEntity> singleLightList) {
        site = (SiteEntity) UserDbController.getInstance(false).queryById(site.getSiteCode(),SiteEntity.class);
        singleLightPresenter.getSingleLightList(this);
        EventBus.getDefault().post(new FragmentRefreshEvent());
    }


}
